/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package aptech.semester.code.gui;

import aptech.semester.code.common.ApplicationCommon;
import aptech.semester.code.common.ImageFileFilter;
import aptech.semester.code.common.Role;
import aptech.semester.code.data.BankList;
import aptech.semester.code.data.DepartmentList;
import aptech.semester.code.data.EducationList;
import aptech.semester.code.data.EmployeeList;
import aptech.semester.code.data.PositionList;
import aptech.semester.code.data.ProvinceList;
import aptech.semester.code.data.SpecializedList;
import aptech.semester.code.entity.BankEntity;
import aptech.semester.code.entity.DepartmentEntity;
import aptech.semester.code.entity.EducationEntity;
import aptech.semester.code.entity.EmployeeEntity;
import aptech.semester.code.entity.PositionEntity;
import aptech.semester.code.entity.ProvinceEntity;
import aptech.semester.code.entity.SpecializedEntity;
import aptech.semester.code.item.BankComboItem;
import aptech.semester.code.item.DepartmentComboItem;
import aptech.semester.code.item.EducationComboItem;
import aptech.semester.code.item.PositionComboItem;
import aptech.semester.code.item.ProvinceComboItem;
import aptech.semester.code.item.SpecializedComboItem;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.RowFilter;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;


/**
 *
 * @author vi
 */
public class EmployeeManagement extends javax.swing.JInternalFrame {

    /**
     * Creates new form EmployeeManagement
     */
    public EmployeeManagement(MainScreen mainFrame) {
        initComponents();
        //leftPanel.setVisible(false);
        initModelViewEmployee();
        disableFillEmployee();
        dcBirthDay.enableInputMethods(false);
        btnReset.setEnabled(false);
        btnCancel.setEnabled(false);
        mainScreen = mainFrame;
        permission(mainFrame.getRole());
        setSize(new Dimension(mainFrame.getWidth() - 5, mainFrame.getHeight() - 91));
        btnCancel.setSize(89, 29);

        setIdIdentity();
        btnDelete.setVisible(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        avatarChooser = new javax.swing.JFileChooser();
        jSplitPane1 = new javax.swing.JSplitPane();
        leftPanel = new javax.swing.JPanel();
        scrollViewEmployee = new javax.swing.JScrollPane();
        tblViewEmployee = new javax.swing.JTable();
        txtSearchEmployee = new javax.swing.JTextField();
        btnSearchEmp = new javax.swing.JButton();
        cbSearchDepartment = new javax.swing.JComboBox();
        btnResetTable = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        rightPanel = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtEmployeeName = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        radMale = new javax.swing.JRadioButton();
        radFemale = new javax.swing.JRadioButton();
        jLabel10 = new javax.swing.JLabel();
        cbProvince = new javax.swing.JComboBox();
        jLabel11 = new javax.swing.JLabel();
        cbBank = new javax.swing.JComboBox();
        jLabel12 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        cbEducation = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();
        lblAvatar = new javax.swing.JLabel();
        jLabel19 = new javax.swing.JLabel();
        radSingle = new javax.swing.JRadioButton();
        radMaried = new javax.swing.JRadioButton();
        cbSpecialized = new javax.swing.JComboBox();
        jLabel16 = new javax.swing.JLabel();
        txtEmployeeID = new javax.swing.JFormattedTextField();
        dcBirthDay = new com.toedter.calendar.JDateChooser();
        ChooseAvatar = new javax.swing.JButton();
        txtAccountNumber = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        jLabel13 = new javax.swing.JLabel();
        cbDepartment = new javax.swing.JComboBox();
        jLabel9 = new javax.swing.JLabel();
        cbPosition = new javax.swing.JComboBox();
        jLabel14 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        dcEntryDate = new com.toedter.calendar.JDateChooser();
        txtBasicSalary = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtAddress = new javax.swing.JTextArea();
        jLabel6 = new javax.swing.JLabel();
        txtEmail = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        txtPhone = new javax.swing.JTextField();
        jPanel5 = new javax.swing.JPanel();
        btnStatus = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        btnReset = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        btnUpdate = new javax.swing.JButton();
        btnInsert = new javax.swing.JButton();

        avatarChooser.setFileFilter(new ImageFileFilter());

        setClosable(true);
        setMaximizable(true);
        setTitle("Employee Management");
        setPreferredSize(new java.awt.Dimension(1080, 600));
        setRequestFocusEnabled(false);

        jSplitPane1.setDividerLocation(350);

        leftPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Employee List"));

        tblViewEmployee.setAutoCreateRowSorter(true);
        tblViewEmployee.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Employee ID", "Emloyee name", "Position", "Department"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblViewEmployee.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                tblViewEmployeeMousePressed(evt);
            }
        });
        scrollViewEmployee.setViewportView(tblViewEmployee);
        if (tblViewEmployee.getColumnModel().getColumnCount() > 0) {
            tblViewEmployee.getColumnModel().getColumn(0).setMinWidth(0);
            tblViewEmployee.getColumnModel().getColumn(0).setPreferredWidth(0);
            tblViewEmployee.getColumnModel().getColumn(0).setMaxWidth(0);
        }

        txtSearchEmployee.setToolTipText("input name of employee");
        txtSearchEmployee.setName(""); // NOI18N
        txtSearchEmployee.setRequestFocusEnabled(false);

        btnSearchEmp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/btn_search.png"))); // NOI18N
        btnSearchEmp.setText("Search");
        btnSearchEmp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchEmpActionPerformed(evt);
            }
        });

        cbSearchDepartment.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "---Select---" }));
        cbSearchDepartment.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbSearchDepartmentActionPerformed(evt);
            }
        });

        btnResetTable.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/reset.gif"))); // NOI18N
        btnResetTable.setText("Reset");
        btnResetTable.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResetTableActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout leftPanelLayout = new javax.swing.GroupLayout(leftPanel);
        leftPanel.setLayout(leftPanelLayout);
        leftPanelLayout.setHorizontalGroup(
            leftPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, leftPanelLayout.createSequentialGroup()
                .addGroup(leftPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(cbSearchDepartment, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(txtSearchEmployee))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnSearchEmp, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addComponent(scrollViewEmployee, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
            .addGroup(leftPanelLayout.createSequentialGroup()
                .addGap(124, 124, 124)
                .addComponent(btnResetTable)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        leftPanelLayout.setVerticalGroup(
            leftPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(leftPanelLayout.createSequentialGroup()
                .addGroup(leftPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtSearchEmployee, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnSearchEmp))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cbSearchDepartment, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(scrollViewEmployee, javax.swing.GroupLayout.DEFAULT_SIZE, 493, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnResetTable)
                .addGap(16, 16, 16))
        );

        txtSearchEmployee.getAccessibleContext().setAccessibleName("");
        txtSearchEmployee.getAccessibleContext().setAccessibleDescription("employee name");

        jSplitPane1.setLeftComponent(leftPanel);

        jScrollPane3.setPreferredSize(new java.awt.Dimension(800, 900));

        rightPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Employee Information"));
        rightPanel.setPreferredSize(new java.awt.Dimension(660, 830));

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Personal Info"));

        jLabel1.setText("Name");

        txtEmployeeName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtEmployeeNameActionPerformed(evt);
            }
        });

        jLabel2.setText("Date of Birth");

        jLabel4.setText("Gender");

        buttonGroup2.add(radMale);
        radMale.setText("Male");

        buttonGroup2.add(radFemale);
        radFemale.setText("Female");

        jLabel10.setText("Province");

        jLabel11.setText("Bank");

        jLabel12.setText("Account Number");

        jLabel3.setText("Education Level");

        jLabel8.setText("Specialize");

        lblAvatar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/avatar.png"))); // NOI18N
        lblAvatar.setText("jLabel18");

        jLabel19.setText("Marital");

        buttonGroup1.add(radSingle);
        radSingle.setText("Single");

        buttonGroup1.add(radMaried);
        radMaried.setText("Maried");

        jLabel16.setText("Employee ID");

        try {
            txtEmployeeID.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("E######")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        ChooseAvatar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/edit.png"))); // NOI18N
        ChooseAvatar.setToolTipText("");
        ChooseAvatar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ChooseAvatarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel10)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3)
                            .addComponent(jLabel19)
                            .addComponent(jLabel8)
                            .addComponent(jLabel11)
                            .addComponent(jLabel12)
                            .addComponent(jLabel16)
                            .addComponent(jLabel2))
                        .addGap(14, 14, 14)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(cbBank, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(cbProvince, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(txtEmployeeName)
                            .addComponent(cbEducation, javax.swing.GroupLayout.Alignment.TRAILING, 0, 240, Short.MAX_VALUE)
                            .addComponent(cbSpecialized, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(txtEmployeeID)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(radMale)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(radFemale))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(radSingle)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(radMaried))
                            .addComponent(dcBirthDay, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(txtAccountNumber))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 244, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addComponent(lblAvatar, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(42, 42, 42))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addComponent(ChooseAvatar)
                        .addGap(72, 72, 72))))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(0, 3, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(lblAvatar, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(ChooseAvatar)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel16)
                            .addComponent(txtEmployeeID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(txtEmployeeName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(dcBirthDay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(3, 3, 3)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(radMale)
                            .addComponent(jLabel4)
                            .addComponent(radFemale))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cbProvince, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel10))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel19)
                            .addComponent(radSingle)
                            .addComponent(radMaried))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cbEducation, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cbSpecialized, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel8))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel11)
                            .addComponent(cbBank, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel12)
                            .addComponent(txtAccountNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Working Info"));

        jLabel13.setText("Department");

        jLabel9.setText("Position");

        jLabel14.setText("Entry Date");

        jLabel15.setText("Basic Salary");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel13)
                    .addComponent(jLabel9)
                    .addComponent(jLabel14)
                    .addComponent(jLabel15))
                .addGap(43, 43, 43)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(cbPosition, javax.swing.GroupLayout.Alignment.TRAILING, 0, 200, Short.MAX_VALUE)
                    .addComponent(cbDepartment, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(dcEntryDate, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(txtBasicSalary))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(cbDepartment, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel13))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbPosition, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel14)
                    .addComponent(dcEntryDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel15)
                    .addComponent(txtBasicSalary, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Contact Info"));

        jLabel5.setText("Address");

        txtAddress.setColumns(20);
        txtAddress.setRows(5);
        jScrollPane2.setViewportView(txtAddress);

        jLabel6.setText("Email");

        jLabel7.setText("Phone Number");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6)
                    .addComponent(jLabel7))
                .addGap(28, 28, 28)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtEmail, javax.swing.GroupLayout.DEFAULT_SIZE, 235, Short.MAX_VALUE)
                            .addComponent(txtPhone))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(txtPhone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder("Command"));

        btnStatus.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/inactive.png"))); // NOI18N
        btnStatus.setText("Inactive");
        btnStatus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStatusActionPerformed(evt);
            }
        });

        btnCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/btn_cancel.png"))); // NOI18N
        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        btnReset.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/btn_reset.png"))); // NOI18N
        btnReset.setText("Reset");
        btnReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResetActionPerformed(evt);
            }
        });

        btnDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/delete.png"))); // NOI18N
        btnDelete.setText("Delete");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });

        btnUpdate.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/edit.png"))); // NOI18N
        btnUpdate.setText("Update");
        btnUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdateActionPerformed(evt);
            }
        });

        btnInsert.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/add.png"))); // NOI18N
        btnInsert.setText("Add New");
        btnInsert.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnInsertActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
                .addContainerGap(183, Short.MAX_VALUE)
                .addComponent(btnInsert)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnUpdate)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnDelete)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnReset, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnCancel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnStatus)
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(btnStatus)
                .addComponent(btnCancel)
                .addComponent(btnReset)
                .addComponent(btnDelete)
                .addComponent(btnUpdate)
                .addComponent(btnInsert))
        );

        javax.swing.GroupLayout rightPanelLayout = new javax.swing.GroupLayout(rightPanel);
        rightPanel.setLayout(rightPanelLayout);
        rightPanelLayout.setHorizontalGroup(
            rightPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(rightPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(rightPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, rightPanelLayout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())
                    .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );
        rightPanelLayout.setVerticalGroup(
            rightPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(rightPanelLayout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(185, Short.MAX_VALUE))
        );

        jScrollPane3.setViewportView(rightPanel);

        jSplitPane1.setRightComponent(jScrollPane3);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 1015, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 642, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSearchEmpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchEmpActionPerformed
////        model = (DefaultTableModel)tblViewEmployee.getModel();
//        
//        DepartmentComboItem dept = (DepartmentComboItem) cbSearchDepartment.getSelectedItem();
//        int deptID = dept.getValue().getDepartmentId();
//        JOptionPane.showMessageDialog(null, deptID);
//
//        List<EmployeeEntity> listEmp = new EmployeeList().getEmployeeByDepartment(deptID);
//        if (listEmp.size() != 0) {
//            model.setRowCount(0);
//            for (EmployeeEntity item : listEmp) {
//                model.addRow(item.toVector1());
//            }
//        } else {
//            JOptionPane.showMessageDialog(null, "Not found any data of employee !");
//        }
        
        String dept =(String) cbSearchDepartment.getSelectedItem().toString();
        String dk = txtSearchEmployee.getText().trim();
        sorter = (TableRowSorter<TableModel>) tblViewEmployee.getRowSorter();
        List filter = new ArrayList();
        if (txtSearchEmployee.getText().trim().equals("") && cbSearchDepartment.getSelectedIndex() == 0) {
            JOptionPane.showMessageDialog(null, "Please input condition to search !");
            txtSearchEmployee.requestFocus();
        } else  {
            
            
            if (!txtSearchEmployee.getText().trim().equals("") && cbSearchDepartment.getSelectedIndex() == 0) {
                filter.add(RowFilter.regexFilter("(?i)"+dk, 1));
                sorter.setRowFilter(RowFilter.andFilter(filter));
                txtSearchEmployee.requestFocus();
            } 
            
            if (txtSearchEmployee.getText().trim().equals("") && cbSearchDepartment.getSelectedIndex() != 0) {
                filter.add(RowFilter.regexFilter("(?i)"+dept, 3));
                sorter.setRowFilter(RowFilter.andFilter(filter));
                txtSearchEmployee.requestFocus();
            } 
            if (!txtSearchEmployee.getText().trim().equals("") && cbSearchDepartment.getSelectedIndex() != 0) {
                filter.add(RowFilter.regexFilter("(?i)"+dk, 1));
                filter.add(RowFilter.regexFilter(dept, 3));
                sorter.setRowFilter(RowFilter.andFilter(filter));
                txtSearchEmployee.requestFocus();
            } 
            if(tblViewEmployee.getRowCount()==0) {
                JOptionPane.showMessageDialog(null, "Not found any result !");
                return;
            
            }
        }

    }//GEN-LAST:event_btnSearchEmpActionPerformed

    private void txtEmployeeNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtEmployeeNameActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtEmployeeNameActionPerformed

    private void tblViewEmployeeMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblViewEmployeeMousePressed
        int r = tblViewEmployee.getSelectedRow();
        if(r<0) {
            return;
        } else {
        String id = tblViewEmployee.getValueAt(r,0).toString();
        selectedEmployee = (EmployeeEntity) mapEmployee.get(id);
        fillData(selectedEmployee);
        if (selectedEmployee.isIsActive()==true) {
                btnStatus.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/inactive.png")));
                btnStatus.setText("Inactive");
            } else {
                btnStatus.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/checked.png")));
                btnStatus.setText("Active");
            }
        }
        
    }//GEN-LAST:event_tblViewEmployeeMousePressed
    File file;
    private void ChooseAvatarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ChooseAvatarActionPerformed
        int returnVal = avatarChooser.showDialog(this, "Choose Avatar");
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            file = avatarChooser.getSelectedFile();
            selectedEmployee.setAvatar(file);
            lblAvatar.setIcon(new ApplicationCommon().getIconForAvatar(file));
        }
    }//GEN-LAST:event_ChooseAvatarActionPerformed
    
    boolean flagInsertSave = false;
    private void btnInsertActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnInsertActionPerformed
           
        if (!flagInsertSave) {
            flagInsertSave = true;
            tblViewEmployee.setEnabled(false);
            btnInsert.setText("Save");
            btnInsert.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/save.png")));
            
            
            resetData();
            setIdIdentity();
            editableFillEmployee();
            btnReset.setEnabled(true);
            btnCancel.setEnabled(true);
            btnUpdate.setEnabled(false);
            btnDelete.setEnabled(false);
            btnStatus.setEnabled(false);
            
                    
        } else {
            if (ValidationEmployee() == true) {
                if (new EmployeeList().insert(prepareData())) {
                    
                    JOptionPane.showMessageDialog(null, "Insert successful !");
                    loadDataTableEmployee();
                } else {
                    JOptionPane.showMessageDialog(null, "Insert fail !");
                }
                disableFillEmployee();
                btnReset.setEnabled(false);
                btnCancel.setEnabled(false);
                tblViewEmployee.setEnabled(true);
                btnUpdate.setEnabled(true);
                btnDelete.setEnabled(true);
                btnStatus.setEnabled(true);
                btnInsert.setText("Add New");
                btnInsert.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/add.png")));
                flagInsertSave = false;
            } else {
                flagInsertSave = true;
            }
        }
    }//GEN-LAST:event_btnInsertActionPerformed
    boolean flagEditSave = false;
    private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateActionPerformed
        int r = tblViewEmployee.getSelectedRow();
        if (r < 0) {
            JOptionPane.showMessageDialog(null, "Please choose row which you want to update!");
        } else {
            editableFillEmployee();
            btnInsert.setEnabled(false);
            btnDelete.setEnabled(false);
//            btnReset.setEnabled(true);
            btnCancel.setEnabled(true);
            btnStatus.setEnabled(false);

            if (!flagEditSave) {
                flagEditSave = true;
                btnUpdate.setText("Save");
                btnUpdate.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/save.png")));
                tblViewEmployee.setEnabled(false);
            } else {
                if (ValidationEmployee() == true) {
                    if (new EmployeeList().update(prepareData())) {
                        JOptionPane.showMessageDialog(null, "Update successful !");
                        loadDataTableEmployee();
                        tblViewEmployee.setEnabled(true);
                        txtSearchEmployee.requestFocus();
                    } else {
                        JOptionPane.showMessageDialog(null, "Update fail !");
                    }
                    disableFillEmployee();
                    btnReset.setEnabled(false);
                    btnCancel.setEnabled(false);
                    btnInsert.setEnabled(true);
                    btnDelete.setEnabled(true);
                    btnStatus.setEnabled(true);
                    btnUpdate.setText("Update");
                    btnUpdate.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/edit.png")));
                    flagEditSave = false;
                } else {
                    flagEditSave = true;
                }
            }
        }

    }//GEN-LAST:event_btnUpdateActionPerformed
    
    private void btnStatusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStatusActionPerformed
        int r = tblViewEmployee.getSelectedRow();
        if (r < 0) {
            JOptionPane.showMessageDialog(null, "Please choose a line which you want to change status!");

        } else {
            int rd = JOptionPane.showConfirmDialog(null, "Are you sure? Please click OK to continue and Cancel to stop!", "Notify", JOptionPane.OK_CANCEL_OPTION);
            if (rd == JOptionPane.OK_OPTION) {
                
                String id = tblViewEmployee.getValueAt(r, 0).toString();
                if (btnStatus.getText().equals("Inactive")) {

                    if (new EmployeeList().changeStatusEmployee(false, id)) {
                        loadDataTableEmployee();
                        JOptionPane.showMessageDialog(null, "Inactive successful !");
                        btnStatus.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/checked.png")));
                        btnStatus.setText("Active");
                    } else {
                        JOptionPane.showMessageDialog(null, "Inactive fail !");
                    }

                } else {

                    if (new EmployeeList().changeStatusEmployee(true, id)) {
                        loadDataTableEmployee();
                        JOptionPane.showMessageDialog(null, "Active successful !");
                        btnStatus.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/inactive.png")));
                        btnStatus.setText("Inactive");
                    } else {
                        JOptionPane.showMessageDialog(null, "Active fail !");
                    }
                }

            }
        }
    }//GEN-LAST:event_btnStatusActionPerformed

    private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnResetActionPerformed
        resetData();
    }//GEN-LAST:event_btnResetActionPerformed

    private void cbSearchDepartmentActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbSearchDepartmentActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cbSearchDepartmentActionPerformed

    private void btnResetTableActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnResetTableActionPerformed
        txtSearchEmployee.setText("");
        cbSearchDepartment.setSelectedIndex(0);
        sorter.setRowFilter(null);
        txtSearchEmployee.requestFocus();
        return;
    }//GEN-LAST:event_btnResetTableActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        int r = JOptionPane.showConfirmDialog(null, "Cancel process?", "Notify", JOptionPane.OK_CANCEL_OPTION);
        if (r == JOptionPane.OK_OPTION) {
                 
//                System.exit(0);
            btnStatus.setEnabled(true);
            tblViewEmployee.setEnabled(true);
            if (flagInsertSave == true) {
                    resetData();
                disableFillEmployee();
                btnInsert.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/add.png")));
                btnInsert.setText("Add New");
                flagInsertSave = false;
                btnReset.setEnabled(false);
                btnCancel.setEnabled(false);
                btnUpdate.setEnabled(true);
                btnDelete.setEnabled(true);
//                tblViewEmployee.setEnabled(true);
                int r1 = tblViewEmployee.getSelectedRow();
                if (r1 >= 0) {
                    String id = tblViewEmployee.getValueAt(r1, 0).toString();
                    selectedEmployee = (EmployeeEntity) mapEmployee.get(id);
                    fillData(selectedEmployee);
                }
            } else {
                if (r < 0) {
                    JOptionPane.showMessageDialog(null, "Please choose row which you want to update!");
                } else {
                    disableFillEmployee();
                    btnUpdate.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/edit.png")));
                    btnUpdate.setText("Update");
                    flagEditSave = false;
                    btnReset.setEnabled(false);
                    btnCancel.setEnabled(false);
                    btnInsert.setEnabled(true);
                    btnDelete.setEnabled(true);
                }
            }

        }
    }//GEN-LAST:event_btnCancelActionPerformed

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        int r = tblViewEmployee.getSelectedRow();
        if (r < 0) {
            JOptionPane.showMessageDialog(null, "Please choose row which you want to delete!");
        } else {
            int rd = JOptionPane.showConfirmDialog(null, "Are you sure?", "Notify", JOptionPane.OK_CANCEL_OPTION);
            if (rd == JOptionPane.OK_OPTION) {
                if (new EmployeeList().delete(selectedEmployee.getEmployeeId())) {
                    loadDataTableEmployee();
                    resetData();
                    JOptionPane.showMessageDialog(null, "Delete successful !");
                    txtSearchEmployee.requestFocus();
                } else {
                    JOptionPane.showMessageDialog(null, "Delete fail !");
                }
            }
        }

    }//GEN-LAST:event_btnDeleteActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton ChooseAvatar;
    private javax.swing.JFileChooser avatarChooser;
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnInsert;
    private javax.swing.JButton btnReset;
    private javax.swing.JButton btnResetTable;
    private javax.swing.JButton btnSearchEmp;
    private javax.swing.JButton btnStatus;
    private javax.swing.JButton btnUpdate;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.JComboBox cbBank;
    private javax.swing.JComboBox cbDepartment;
    private javax.swing.JComboBox cbEducation;
    private javax.swing.JComboBox cbPosition;
    private javax.swing.JComboBox cbProvince;
    private javax.swing.JComboBox cbSearchDepartment;
    private javax.swing.JComboBox cbSpecialized;
    private com.toedter.calendar.JDateChooser dcBirthDay;
    private com.toedter.calendar.JDateChooser dcEntryDate;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JLabel lblAvatar;
    private javax.swing.JPanel leftPanel;
    private javax.swing.JRadioButton radFemale;
    private javax.swing.JRadioButton radMale;
    private javax.swing.JRadioButton radMaried;
    private javax.swing.JRadioButton radSingle;
    private javax.swing.JPanel rightPanel;
    private javax.swing.JScrollPane scrollViewEmployee;
    private javax.swing.JTable tblViewEmployee;
    private javax.swing.JTextField txtAccountNumber;
    private javax.swing.JTextArea txtAddress;
    private javax.swing.JTextField txtBasicSalary;
    private javax.swing.JTextField txtEmail;
    private javax.swing.JFormattedTextField txtEmployeeID;
    private javax.swing.JTextField txtEmployeeName;
    private javax.swing.JTextField txtPhone;
    private javax.swing.JTextField txtSearchEmployee;
    // End of variables declaration//GEN-END:variables
    
    DefaultTableModel model; 
    TableRowSorter<TableModel> sorter;
    private TreeMap mapEmployee = new TreeMap<String, EmployeeEntity>();
    private EmployeeEntity selectedEmployee = new EmployeeEntity() ;
    private List<ProvinceEntity> listProvince;
    private List<EducationEntity> listEducation;
    private List<BankEntity> listBank;
    private List<SpecializedEntity> listSpecialized;
    private List<PositionEntity> listPos;
    private List<DepartmentEntity> listDept;
    private MainScreen mainScreen;
    private void initModelViewEmployee() {
        sorter = (TableRowSorter<TableModel>) tblViewEmployee.getRowSorter();;
        this.loadDataTableEmployee();
        // binding data for department
        listDept = new DepartmentList().getAllDepartment();
        for (DepartmentEntity dept : listDept) {
            cbDepartment.addItem(new DepartmentComboItem(dept)); 
            cbSearchDepartment.addItem(new DepartmentComboItem(dept));
        }
        
        // binding data for position
        listPos = new PositionList().getAllPosition();
        for (PositionEntity pos : listPos) {
            cbPosition.addItem(new PositionComboItem(pos));
        }
        
        // binding data for bank
        listBank = new BankList().getAllBank();
        for (BankEntity bank : listBank) {
            cbBank.addItem(new BankComboItem(bank));
        }
        
        // binding data for province
        listProvince = new ProvinceList().getAllProvince();
        for (ProvinceEntity province : listProvince) {
            cbProvince.addItem(new ProvinceComboItem(province));
        }
        
        // binding data for education
        listEducation = new EducationList().getAllEducationLevel();
        for (EducationEntity education : listEducation) {
            cbEducation.addItem(new EducationComboItem(education));
        }
        
        // binding data for specialized
       listSpecialized = new SpecializedList().getAllSpecialized();
        for (SpecializedEntity specialized : listSpecialized) {
            cbSpecialized.addItem(new SpecializedComboItem(specialized));
        }
    }
    
    private void disableFillEmployee() {
        txtEmployeeID.setEditable(false);
        txtEmployeeName.setEditable(false);
        dcBirthDay.setEnabled(false);
        cbProvince.setEnabled(false);
        cbEducation.setEnabled(false);
        cbSpecialized.setEnabled(false);
        cbBank.setEnabled(false);
        txtAccountNumber.setEditable(false);
        ChooseAvatar.setEnabled(false);
        txtAddress.setEditable(false);
        txtEmail.setEditable(false);
        txtPhone.setEditable(false);
        cbDepartment.setEnabled(false);
        cbPosition.setEnabled(false);
        dcEntryDate.setEnabled(false);
        txtBasicSalary.setEditable(false);
        radFemale.setEnabled(false);
        radMale.setEnabled(false);
        radMaried.setEnabled(false);
        radSingle.setEnabled(false);
    }
    private void editableFillEmployee() {
        txtEmployeeName.setEditable(true);
        dcBirthDay.setEnabled(true);
        radFemale.setEnabled(true);
        radMale.setEnabled(true);
        cbProvince.setEnabled(true);
        radMaried.setEnabled(true);
        radSingle.setEnabled(true);
        cbEducation.setEnabled(true);
        cbSpecialized.setEnabled(true);
        cbBank.setEnabled(true);
        txtAccountNumber.setEditable(true);
        ChooseAvatar.setEnabled(true);
        txtAddress.setEditable(true);
        txtEmail.setEditable(true);
        txtPhone.setEditable(true);
        cbDepartment.setEnabled(true);
        cbPosition.setEnabled(true);
        dcEntryDate.setEnabled(true);
        txtBasicSalary.setEditable(true);
        
        
    }
    
    private void resetData() {
        txtEmployeeID.setText("");
        txtEmployeeName.setText("");
        dcBirthDay.setDate(null);
        buttonGroup2.clearSelection();
        cbProvince.setSelectedIndex(0);
        buttonGroup1.clearSelection();
        cbEducation.setSelectedIndex(0);
        cbSpecialized.setSelectedIndex(0);
        cbBank.setSelectedIndex(0);
        txtAccountNumber.setText("");
        lblAvatar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/avatar.png")));
        txtAddress.setText("");
        txtEmail.setText("");
        txtPhone.setText("");
        cbDepartment.setSelectedIndex(0);
        cbPosition.setSelectedIndex(0);
        dcEntryDate.setDate(null);
        txtBasicSalary.setText("");
    }
    private boolean ValidationEmployee() {
        if(txtEmployeeID.getText().trim().length()!=7) {
            JOptionPane.showMessageDialog(null, "Enter wrong format E######!");
            txtEmployeeID.requestFocus();
            return false;
        }
        if(txtEmployeeName.getText().trim().isEmpty()) {
            JOptionPane.showMessageDialog(null, "Employee Name can not blank!");
            txtEmployeeName.requestFocus();
            return false;
        }
        if(txtEmployeeName.getText().trim().matches("^[\\d]+$")){
            JOptionPane.showMessageDialog(null, "Employee Name can not numeric!");
            txtEmployeeName.requestFocus();
            return false;
        }

        Date d1 = dcBirthDay.getDate();

        if(d1==null) {
            JOptionPane.showMessageDialog(null, "Please choose date of birth");
            dcBirthDay.requestFocus();
            return false;
        }
        Calendar localCalendar = Calendar.getInstance(TimeZone.getDefault());
        int currentYear = localCalendar.get(Calendar.YEAR);
        DateFormat d = new SimpleDateFormat("yyyy/mm/dd");
        String d2 = d.format(d1).substring(0,4);
        if (((currentYear - Integer.parseInt(d2)) < 18) || ((currentYear - Integer.parseInt(d2))>60)) {
            JOptionPane.showMessageDialog(null, "Please choose date of birth suitable !");
            dcBirthDay.requestFocus();
            return false;
        }
        
        
        if(radFemale.isSelected()==false && radMale.isSelected() == false) {
            JOptionPane.showMessageDialog(null, "Please choose gender!");
            radMale.requestFocus();
            return false;
        }
        if(txtAccountNumber.getText().trim().isEmpty()) {
            JOptionPane.showMessageDialog(null, "Account number can not blank & must be numeric");
            txtAccountNumber.requestFocus();
            return false;
        }
        if(!txtAccountNumber.getText().trim().matches("^[\\d]+$")) {
            JOptionPane.showMessageDialog(null, "Input account number invalid!");
            txtAccountNumber.requestFocus();
            return false;
        }
        if(txtAccountNumber.getText().trim().length()>20 || txtAccountNumber.getText().trim().length()<9) {
            JOptionPane.showMessageDialog(null, "Account number must be from 9 to 20 characters");
            txtAccountNumber.requestFocus();
            return false;
        }
        
       String avatar = lblAvatar.getIcon().toString();
       String a = avatar.substring(avatar.lastIndexOf("/")+1);
       if(a.equals("avatar.png")) {
           JOptionPane.showMessageDialog(null, "Please choose avatar!");
           ChooseAvatar.requestFocus();
           return false;
       }
       
   
        if(txtAddress.getText().trim().isEmpty()) {
            JOptionPane.showMessageDialog(null, "Address can not blank!");
            txtAddress.requestFocus();
            return false;
        }
        if(txtAddress.getText().trim().matches("^[\\d]+$")){
            JOptionPane.showMessageDialog(null, "Address can not numeric!");
            txtAddress.requestFocus();
            return false;
        }
        
//        Pattern pattern = Pattern.compile("^[\\w]+@[\\w]{2,6}\\.[\\w]{2,6}$");
        if(txtEmail.getText().trim().isEmpty()) {
            JOptionPane.showMessageDialog(null, "Email can not blank!");
            txtEmail.requestFocus();
            return false;
        }
        if(!txtEmail.getText().matches("^[\\w]+@[\\w]{2,6}(\\.[\\a-zA-Z]{2,6}){1,2}$")) {
            JOptionPane.showMessageDialog(null, "Please enter email follow format xxxx@xxxx.xxx. Example huynhdiemlx@gmail.com!");
            txtEmail.requestFocus();
            return false;
        }

        if(txtPhone.getText().trim().isEmpty()) {
            JOptionPane.showMessageDialog(null, "Phone can not blank and must be number!");
            txtPhone.requestFocus();
            return false;
        }
        if(!txtPhone.getText().trim().matches("^[\\d]+$")){
            JOptionPane.showMessageDialog(null, "Input phone invalid!");
            txtPhone.requestFocus();
            return false;
        }
        if(txtPhone.getText().trim().length()>12 || txtPhone.getText().trim().length()<9 ) {
            JOptionPane.showMessageDialog(null, "Phone must be from 10 to 12 characters!");
            txtPhone.requestFocus();
            return false;
        }
        
        
        if(dcEntryDate.getDate()==null) {
            JOptionPane.showMessageDialog(null, " Please choose entry date!");
            dcEntryDate.requestFocus();
            return false;
        }
        Date d4 = dcEntryDate.getDate();
        String d3 = d.format(d4).substring(0,4);
         
        if(Integer.parseInt(d3)-Integer.parseInt(d2)<18) {
            JOptionPane.showMessageDialog(null, " Please choose entry date suitable!");
            dcEntryDate.requestFocus();
            return false;
        }
        
        if(txtBasicSalary.getText().trim().isEmpty()) {
            JOptionPane.showMessageDialog(null, "Basic salary can not blank and must be number!");
            txtBasicSalary.requestFocus();
            return false;
        }
        if(!txtBasicSalary.getText().trim().matches("^[\\d]+$")){
            JOptionPane.showMessageDialog(null, "Input basic salary invalid!");
            txtBasicSalary.requestFocus();
            return false;
        }
        if(txtBasicSalary.getText().trim().length() > 8 || txtBasicSalary.getText().trim().length() < 6) {
            JOptionPane.showMessageDialog(null, "Please Input basic salary suitable! Check again!");
            txtBasicSalary.requestFocus();
            return false;
        }

        return true;
    }
    
    private void fillData(EmployeeEntity emp) {
        txtEmployeeID.setText(emp.getEmployeeId());
        txtEmployeeName.setText(emp.getName());
        dcBirthDay.setDate(emp.getBirthday());
        if(emp.isGender()==true) {
            radMale.setSelected(true);
        } else {
            radFemale.setSelected(true);
        }
        
        
        cbProvince.setSelectedIndex(getIndexOfProvince(emp.getProvinceName()));
        radSingle.setSelected(!emp.isMaritalStatus());
        radMaried.setSelected(emp.isMaritalStatus());
        cbEducation.setSelectedIndex(getIndexOfEducation(emp.getEducationLevel()));
        cbSpecialized.setSelectedIndex(getIndexOfSpecialized(emp.getSpecialize()));
        cbBank.setSelectedIndex(getIndexOfBank(emp.getBankName()));
        txtAccountNumber.setText(emp.getAccountNumberBank()+"");
        lblAvatar.setIcon(new ApplicationCommon().getIconForAvatar(emp.getAvatar()));
        
        txtAddress.setText(emp.getAddress());
        txtEmail.setText(emp.getEmail());
        txtPhone.setText(emp.getPhone()+"");
        
        cbDepartment.setSelectedIndex(getIndexOfDept(emp.getDepartmentName()));
        cbPosition.setSelectedIndex(getIndexOfPos(emp.getPositionName()));
        dcEntryDate.setDate(emp.getEntryDate());
        txtBasicSalary.setText(emp.getBasicSalary().toBigInteger()+"");
    }
    
    private EmployeeEntity prepareData() {
        EmployeeEntity emp = new EmployeeEntity();
        
        emp.setEmployeeId(txtEmployeeID.getText());
        emp.setName(txtEmployeeName.getText());
        emp.setBirthday(dcBirthDay.getDate());
        if (radMale.isSelected()) {
            emp.setGender(true);
        } else {
            emp.setGender(false);
        }
        ProvinceComboItem provinceItem = (ProvinceComboItem)cbProvince.getSelectedItem();
//        emp.setProvinceName(provinceItem.getValue().getProvinceName());
        emp.setProvinceId(provinceItem.getValue().getProvinceId());
        if (radMaried.isSelected()) {
            emp.setMaritalStatus(true);
        } else {
            emp.setMaritalStatus(false);
        }
        EducationComboItem eduItem = (EducationComboItem)cbEducation.getSelectedItem();
//        emp.setEducationLevel(eduItem.getValue().getEducationName());
        emp.setEducationLevelId(eduItem.getValue().getEducationId());
        SpecializedComboItem specItem = (SpecializedComboItem)cbSpecialized.getSelectedItem();
//        emp.setSpecialize(specItem.getValue().getSpecializedName());
        emp.setSpecializeId(specItem.getValue().getSpecializedId());
        BankComboItem bankItem = (BankComboItem)cbBank.getSelectedItem();
//        emp.setBankName(bankItem.getValue().getBankName());
        emp.setBankId(bankItem.getValue().getBankId());
        emp.setAccountNumberBank(txtAccountNumber.getText());
        emp.setAvatar(selectedEmployee.getAvatar());
        emp.setAddress(txtAddress.getText());
        emp.setPhone(txtPhone.getText()+"");
        emp.setEmail(txtEmail.getText());
        DepartmentComboItem deptItem = (DepartmentComboItem)cbDepartment.getSelectedItem();
//        emp.setDepartmentName(deptItem.getValue().getDepartmentName());
        emp.setDepartmentId(deptItem.getValue().getDepartmentId());
        PositionComboItem posItem = (PositionComboItem)cbPosition.getSelectedItem();
//        emp.setPositionName(posItem.getValue().getPositionName());
        emp.setPositionId(posItem.getValue().getPositionId());
        emp.setEntryDate(dcEntryDate.getDate());
        emp.setBasicSalary(new BigDecimal(Integer.parseInt(txtBasicSalary.getText())));
        return emp;
    }
    
    private int getIndexOfProvince(String name) {
        int index = 0;
        for (int i = 0; i< listProvince.size(); i++) {
            if (listProvince.get(i).getProvinceName().equals(name)) {
                index = i;
                break;
            }
        }
        return index;
    }
    
    private int getIndexOfEducation(String name) {
        int index = 0;
        for (int i = 0; i< listEducation.size(); i++) {
            if (listEducation.get(i).getEducationName().equals(name)) {
                index = i;
                break;
            }
        }
        return index;
    }
    
    private int getIndexOfSpecialized(String name) {
        int index = 0;
        for (int i = 0; i< listSpecialized.size(); i++) {
            if (listSpecialized.get(i).getSpecializedName().equals(name)) {
                index = i;
                break;
            }
        }
        return index;
    }
    
    private int getIndexOfBank(String name) {
        int index = 0;
        for (int i = 0; i< listBank.size(); i++) {
            if (listBank.get(i).getBankName().equals(name)) {
                index = i;
                break;
            }
        }
        return index;
    }
    
    private int getIndexOfDept(String name) {
        int index = 0;
        for (int i = 0; i< listDept.size(); i++) {
            if (listDept.get(i).getDepartmentName().equals(name)) {
                index = i;
                break;
            }
        }
        return index;
    }
    
    private int getIndexOfPos(String name) {
        int index = 0;
        for (int i = 0; i< listPos.size(); i++) {
            if (listPos.get(i).getPositionName().equals(name)) {
                index = i;
                break;
            }
        }
        return index;
    }
    
    private void loadDataTableEmployee() {
        // binding data for employee list      
        model = (DefaultTableModel)tblViewEmployee.getModel();
        model.setRowCount(0);
        List<EmployeeEntity> listEmp = new EmployeeList().getList();
        for (EmployeeEntity item : listEmp) {
            mapEmployee.put(item.getEmployeeId(), item);
            model.addRow(item.toVector1());
        }
        
    }
    
    private void permission(int role) {
        if (role == Role.ROLE_HR) {
            btnDelete.setEnabled(false);
        } else if (role == Role.ROLE_MANAGER) {
            btnCancel.setEnabled(false);
            btnDelete.setEnabled(false);
            btnInsert.setEnabled(false);
            btnUpdate.setEnabled(false);
            btnStatus.setEnabled(false);
        }
    }

    private void setIdIdentity() {
        String s = new EmployeeList().topId();
        if(s!=null)
        {
            String x = s.replace("E","");
            int a = Integer.parseInt(x)+1;
            String p = String.format("%06d",a);
            txtEmployeeID.setText("E"+p);
        }
        else
        {
            int a = 1;
            String p = String.format("%06d",a);
            txtEmployeeID.setText("E"+p);
        }
    }
        
    
}
